package com.example.excel.service.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.export.ExcelBatchExportService;
import cn.afterturn.easypoi.util.PoiMergeCellUtil;
import org.apache.poi.ss.usermodel.*;

/**
 * @author Aaron
 * @since 2021/7/19 16:45
 * <p>描述：</p>
 */
public class ExcelBatchExportServiceImpl extends ExcelBatchExportService {

    private static final ThreadLocal<ExcelBatchExportServiceImpl> THREAD_LOCAL = new ThreadLocal<>();

    @Override
    public int createTitle2Row(ExportParams entity, Sheet sheet, Workbook workbook, int fieldWidth) {
        Row row = sheet.createRow(0);
        row.setHeight(entity.getTitleHeight());
        createStringCell(row, 0, entity.getTitle(),
                getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
        for (int i = 1; i <= fieldWidth; i++) {
            createStringCell(row, i, "",
                    getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
        }
        PoiMergeCellUtil.addMergedRegion(sheet, 0, 0, 0, fieldWidth);

        if (entity.getSecondTitle() != null) {
            row = sheet.createRow(1);
            row.setHeight(entity.getSecondTitleHeight());

            createStringCell(row, 0, entity.getSecondTitle(), getSecondTitleStyle(workbook), null);
            for (int i = 1; i <= fieldWidth; i++) {
                createStringCell(row, i, "",
                        getSecondTitleStyle(workbook), null);
            }
            PoiMergeCellUtil.addMergedRegion(sheet, 1, 1, 0, fieldWidth);
            return 2;
        }

        return 1;
    }

    public static ExcelBatchExportService getExcelBatchExportService(ExportParams entity,
                                                                     Class<?> pojoClass) {
        if (THREAD_LOCAL.get() == null) {
            ExcelBatchExportServiceImpl batchServer = new ExcelBatchExportServiceImpl();
            batchServer.init(entity, pojoClass);
            THREAD_LOCAL.set(batchServer);
        }
        return THREAD_LOCAL.get();
    }

    @Override
    public void closeExportBigExcel() {
        super.closeExportBigExcel();
        THREAD_LOCAL.remove();
    }

    public static CellStyle getSecondTitleStyle(Workbook workbook) {
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setAlignment(HorizontalAlignment.LEFT);
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时自动换行
        titleStyle.setWrapText(true);

        // 字体、大小、加粗
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 10);
        font.setBold(false);
        titleStyle.setFont(font);

        // 设置颜色
        titleStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 边框
        titleStyle.setBorderRight(BorderStyle.THIN);
        titleStyle.setBorderLeft(BorderStyle.THIN);
        titleStyle.setBorderTop(BorderStyle.THIN);
        titleStyle.setBorderBottom(BorderStyle.THIN);

        return titleStyle;
    }

}
